<?php

$id = empty($this->Param['id']) ? 0 : $this->Param['id'];

$this->LoadHelper('RegionHelper');
$RegionHelper = new RegionHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('RoleHelper');
$RoleHelper = new RoleHelper();

if (empty($this->Param['option'])) {
    $data = $RegionHelper->regionRow($id);
    $this->Assign('id', $id);
    $this->Assign('data', $data);
    echo $this->GetView('edit.php');
} else {
    switch ($this->Param['option']) {
        case 'save':
            if($id > 0){
                $result = $RegionHelper->regionSave($this->Param, $id);
            }else{
                $result = $RegionHelper->regionSave($this->Param);
            }
            if($result['success']){
                $desc = $id > 0 ? '修改大区信息' : '新增大区信息';
                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        case 'remove':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $result = $RegionHelper->regionRemove($id);
            if($result){
                $MemberHelper->saveLog($this->UserConfig, '删除大区', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('删除成功');
            }
            ErrorMsg::Debug('删除失败');
            break;
        case 'userlist':
            $dp_id = empty($this->Param['dp_id']) ? ErrorMsg::Debug('参数错误') : $this->Param['dp_id'];
            $dp_id_info = explode('_', $dp_id); 
            $this->Assign('dp_id', $dp_id);
            $ajax = $this->Param['ajax'];
            $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
            $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
            $role_msg = $RoleHelper->roleName(array('`r_status` =?' =>'ok'));
            if ($ajax) {
                
                $where = array(
                    '`u_dp_type` = ?' => 'rg',
                    '`u_dp_id` = ?' => $dp_id_info[1]
                );
                $data = $MemberHelper->pageList($where, $page, $rp, $this->Param);
                $json_data = array('total' => $data['count'], 'rows' => array());
                if(!empty($data['all'])){
                    foreach ($data['all'] AS $rowNum => $row) {
                        $dpinfo = $MemberHelper->getDpinfo($row);
                        $row['u_dpname'] = $dpinfo['dpname'];
                        $row['u_role'] = $role_msg[$row['u_role']];
                        $row['u_addtime'] = date('Y-m-d H:i:s', $row['u_addtime']);
                        $json_data['rows'][] = $row;
                    }
                }
                echo json_encode($json_data);
            } else {
                $member_info = $MemberHelper->getMember($this->UserConfig['Id']);
                $this->Assign('member_info', $member_info);
                echo $this->GetView('userlist.php');
            }
            break;
        default:
    }
    
    
}